import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerFamilyInvestmentApi } from '#/api/per/familyInvestment';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        placeholder: '请选择与本人关系',
      },
    },
    {
      fieldName: 'familyName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'investmentUnit',
      label: '投资单位',
      component: 'Input',
      componentProps: {
        placeholder: '请输入投资或入股单位',
      },
    },
    {
      fieldName: 'countryCity',
      label: '投资的国家',
      component: 'Input',
      componentProps: {
        placeholder: '请输入投资的国家地区及城市',
      },
    },
    {
      fieldName: 'investmentMethod',
      label: '投资方式',
      component: 'Input',
      componentProps: {
        placeholder: '请输入投资或入股方式',
      },
    },
    {
      fieldName: 'position',
      label: '担任职务',
      component: 'Input',
      componentProps: {
        placeholder: '请输入担任职务',
      },
    },
    {
      fieldName: 'investmentDetails',
      label: '投资情况',
      component: 'Input',
      componentProps: {
        placeholder: '请输入投资情况',
      },
    },
    {
      fieldName: 'currency',
      label: '投资币种',
      component: 'Input',
      componentProps: {
        placeholder: '请输入投资币种',
      },
    },
    {
      fieldName: 'amountInvested',
      label: '出资金额',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入出资金额',
        addonAfter: '万元',
      },
    },
    {
      fieldName: 'amountRmb',
      label: '人民币金额',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入人民币金额',
        addonAfter: '万元',
      },
    },
    {
      fieldName: 'annualReturn',
      label: '年收益',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入年收益',
        addonAfter: '万元',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '与本人关系',
      field: 'relation',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.PER_RELATION).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '系统人员ID，对应 system_person.id',
      field: 'systemPersonId',
    },
    {
      label: '姓名',
      field: 'familyName',
    },
    {
      label: '证件类型',
      field: 'cardType',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '身份证号',
      field: 'familyIdcard',
    },
    {
      label: '投资或入股单位',
      field: 'investmentUnit',
    },
    {
      label: '投资的国家地区及城市',
      field: 'countryCity',
    },
    {
      label: '投资或入股方式',
      field: 'investmentMethod',
    },
    {
      label: '担任职务',
      field: 'position',
    },
    {
      label: '投资情况',
      field: 'investmentDetails',
    },
    {
      label: '投资币种',
      field: 'currency',
    },
    {
      label: '出资金额',
      field: 'amountInvested',
    },
    {
      label: '人民币金额',
      field: 'amountRmb',
    },
    {
      label: '年收益',
      field: 'annualReturn',
    },
    {
      label: '备注',
      field: 'remark',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '状态',
      field: 'status',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.COMMON_STATUS).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '创建时间',
      field: 'createTime',
      render: (val) => formatDate(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
    {
      label: '更新时间',
      field: 'updateTime',
      render: (val) => formatDate(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        allowClear: true,
        placeholder: '请选择与本人关系',
      },
    },
    {
      fieldName: 'familyName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'investmentUnit',
      label: '投资单位',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入投资或入股单位',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerFamilyInvestmentApi.FamilyInvestment>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'relation',
      title: '与本人关系',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_RELATION },
      },
    },
    {
      field: 'familyName',
      title: '姓名',
      minWidth: 120,
    },
    {
      field: 'cardType',
      title: '证件类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'familyIdcard',
      title: '身份证号',
      minWidth: 120,
    },
    {
      field: 'investmentUnit',
      title: '投资单位',
      minWidth: 120,
    },
    {
      field: 'countryCity',
      title: '投资的国家',
      minWidth: 120,
    },
    {
      field: 'investmentMethod',
      title: '投资方式',
      minWidth: 120,
    },
    {
      field: 'position',
      title: '担任职务',
      minWidth: 120,
    },
    {
      field: 'investmentDetails',
      title: '投资情况',
      minWidth: 120,
    },
    {
      field: 'currency',
      title: '投资币种',
      minWidth: 120,
    },
    {
      field: 'amountInvested',
      title: '出资金额',
      minWidth: 120,
    },
    {
      field: 'amountRmb',
      title: '人民币金额',
      minWidth: 120,
    },
    {
      field: 'annualReturn',
      title: '年收益',
      minWidth: 120,
    },

    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },

    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
